﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using Demo.CommonBase.DTO.Filters;
using Demo.CommonBase.DTO.Infos;
using Demo.CommonBase.Exceptions;
using Demo.CommonBase.Integration.DAO.Impl;
using Demo.CommonBase.Integration.DAO.IntegrationComponents.Mappings;
using Demo.CommonBase.Logging;
using Demo.Services.Contracts.DTO.Filters;
using Demo.Services.Contracts.DTO.Infos;
using Demo.Services.Integration.DAO.Contracts.Exceptions;
using Demo.Services.Integration.DAO.IntegrationComponents.Helpers;
using Demo.Services.Integration.DAO.IntegrationComponents.Mappings;

namespace Demo.Services.Integration.DAO.Impl
{
    public class ResourceDAO : DAOBase
    {
        public ResourceResultInfo GetResources(PaginationInfo pagin, ResourceFilter filters)
        {
            ResourceResultInfo resourceList = null;
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.GetResourceByPage(pagin, filters);
                DataSet ds = Retrieve("Resource_R", parameters);
                if (ValidateDataSet(ds))
                {
                    resourceList = ResourceDAOMapping.GetResourceList(ds);
                }
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
            return resourceList;
        }

        public ResourceInfo GetResourceById(int id)
        {
            ResourceInfo result = null;
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.GetResourceByIdParams(id);
                DataSet ds = Retrieve("Resource_ById", parameters);
                if (ValidateDataSet(ds))
                {
                    result = ResourceDAOMapping.GetResourceById(ds);
                }
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
            return result;
        }

        public void DeleteResource(int id)
        {
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.GetResourceByIdParams(id);
                Delete("Resource_D", parameters);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
        }

        public void UpdateResource(ResourceInfo resource)
        {
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.GetResourceUpdateParams(resource);
                Update("Resource_U", parameters);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
        }

        public int SaveResource(ResourceInfo resource)
        {
            int id;
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.GetResourceSaveParams(resource);
                id = Create("Resource_C", parameters);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
            return id;
        }

        public void SaveResourceDetail(int id, string tecnologyList)
        {
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.GetResourceSaveDetailParams(id, tecnologyList);
                Create("Resource_C_Detail", parameters);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
        }

        public CommonComboBoxPagingInfo GetRersource_ByName(ComboBoxFilter filter)
        {
            CommonComboBoxPagingInfo resources = null;
            IList<IDbDataParameter> paramsList;
            try
            {
                paramsList = ResourceDAOHelper.GetResourceByNameParams(filter);
                DataSet ds = Retrieve("Resource_R_ByName", paramsList);
                if (ValidateDataSet(ds))
                {
                    resources = CommonComboBoxDAOMapping.GetDataByNameMapping(ds);
                }
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
            return resources;
        }

        public List<TecnologyInfo> GetAssignedTecnologies(int resourceId)
        {
            List<TecnologyInfo> tecnologyList = null;
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.GetResourceByIdParams(resourceId);
                DataSet ds = Retrieve("Resource_R_AssignedTecnology", parameters);
                if (ValidateDataSet(ds))
                {
                    tecnologyList = TecnologyDAOMapping.GetTecnologies(ds);
                }
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
            return tecnologyList;
        }

        public List<TecnologyInfo> GetUnAssignedTecnologies(int resourceId)
        {
            List<TecnologyInfo> tecnologyList = null;
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.GetResourceByIdParams(resourceId);
                DataSet ds = Retrieve("Resource_R_UnAssignedTecnology", parameters);
                if (ValidateDataSet(ds))
                {
                    tecnologyList = TecnologyDAOMapping.GetTecnologies(ds);
                }
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
            return tecnologyList;
        }

        public List<ProjectReportInfo> GetResourceAssignment()
        {
            List<ProjectReportInfo> result = null;
            try
            {
                DataSet ds = Retrieve("Resourse_Graphic_Assignment");
                if (ValidateDataSet(ds))
                {
                    result = ResourceDAOMapping.GetResourceAssignment(ds);
                }
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
            return result;
        }

        public bool  ValidationResource(ResourceFilter filter)
        {
            bool result;
            try
            {
                IList<IDbDataParameter> parameters = ResourceDAOHelper.ValidationResourceParams(filter);
                DataSet ds = Retrieve("Recurso_Validar", parameters);
                result = ValidateDataSet(ds);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                               "ResourceDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
            return result;
        }

        

    }
}